<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="消费组" prop="consumerProxyGroup">
        <el-input
          v-model="queryParams.consumerProxyGroup"
          placeholder="请输入消费组名"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="topic" prop="topic">
        <el-input
          v-model="queryParams.topic"
          placeholder="请输入topic"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="delFlag">
        <el-select
          v-model="queryParams.delFlag"
          placeholder="生产者状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.del_flag"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mq:consumer:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mq:consumer:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="single"
          @click="handleDelete"
          v-hasPermi="['mq:consumer:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="实例" align="center" prop="instanceId" >
        <template slot-scope="scope">
          <span>{{ scope.row.instanceId | instanceFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column label="消费组" align="center" prop="consumerProxyGroup" />
      <el-table-column label="topic" align="center" prop="topic" />
      <el-table-column label="tags" align="center" prop="tags" />
      <el-table-column label="消费类型" align="center" prop="msgType" >
        <template slot-scope="scope">
          <span>{{ scope.row.msgType | msgTypeFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column label="消费端地址" align="center" prop="appUrl" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.del_flag" :value="scope.row.delFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改人" align="center" prop="updateBy" />
      <el-table-column label="修改时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope" v-if="scope.row.delFlag === '0'">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mq:consumer:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mq:consumer:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改消费者对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="MQ实例" prop="instanceId">
          <el-select v-model="form.instanceId" placeholder="请选择MQ实例" >
            <el-option
              v-for="item in instanceOptions"
              :key="item.id"
              :label="item.instanceName"
              :value="item.id"
              :disabled="item.delFlag == '2'"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="消费组" prop="consumerProxyGroup">
          <el-input v-model="form.consumerProxyGroup" placeholder="请输入消费组名" :disabled="formUpdateType" />
        </el-form-item>
        <el-form-item label="topic" prop="topic">
          <el-input v-model="form.topic" placeholder="请输入topic" :disabled="formUpdateType" />
        </el-form-item>
        <el-form-item label="tags" prop="tags">
          <el-input v-model="form.tags" placeholder="请输入tags" />
        </el-form-item>
        <el-form-item label="appUrl" prop="appUrl" placeholder="请输入消费端地址">
          <el-input type="textarea" v-model="form.appUrl"/>
        </el-form-item>
        <el-form-item label="消费类型" prop="msgType" placeholder="请选择消费类型" >
          <el-radio-group v-model="form.msgType">
            <el-radio label="CONCURRENTLY">普通消息</el-radio>
            <el-radio label="ORDERLY">顺序消息</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listConsumer, updateConsumer, addConsumer, delConsumer, getConsumer } from '@/api/mq/consumer'
import { listInstance } from '@/api/mq/instance'
let instanceOptions = []
const typeOptions = [
  { key: 'CONCURRENTLY', value: '普通消费' },
  { key: 'ORDERLY', value: '顺序消费' }
]
export default {
  name: "Consumer",
  dicts: ['del_flag'],
  filters: {
    msgTypeFilter(type) {
      let value = ''
      typeOptions.forEach(function (element, index, arr1) {
        if (type === element.key) {
          value = element.value
        }
      })
      return value
    },
    instanceFilter(instanceId) {
      let value = ''
      instanceOptions.forEach(function (element, index, arr1) {
        if (instanceId === element.id) {
          value = element.instanceName
        }
      })
      return value
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 是否显示弹出层
      open: false,
      //form 类型
      formUpdateType: false,
      // 总条数
      total: 0,
      // 生产者表格数据
      dataList: [],
      instanceOptions: [],
      // 弹出层标题
      title: "",
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        consumerProxyGroup: undefined,
        topic: undefined,
        tags: undefined,
        delFlag: undefined,
      },
      queryInstanceParams: {
        pageNum: 1,
        pageSize: 999999
      },
      rules: {
        instanceId: [{ required: true, message: '必填*', trigger: 'select' }],
        consumerProxyGroup: [{ required: true, message: '必填*', trigger: 'blur' }],
        topic: [{ required: true, message: '必填*', trigger: 'blur' }],
        tags: [{ required: true, message: '必填*', trigger: 'blur' }],
        msgType: [{ required: true, message: '必填*', trigger: 'select' }],
        appUrl: [{ required: true, message: '必填*', trigger: 'blur' }]
      }
    }

  },
  created() {
    listInstance(this.queryInstanceParams).then(response => {
      instanceOptions = response.rows;
      this.instanceOptions = response.rows;
      this.queryParams.pageNum = 1;
      this.getList();
    });
  },
  methods: {
    getList() {
      this.loading = true;
      listConsumer(this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        instanceId: undefined,
        consumerProxyGroup: undefined,
        topic: undefined,
        tags: undefined,
        appUrl: undefined
      };
      this.resetForm("form");
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      listInstance(this.queryInstanceParams).then(response => {
        instanceOptions = response.rows;
        this.instanceOptions = response.rows;
        this.reset();
        this.open = true;
        this.formUpdateType = false
        this.title = "添加消费者";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getConsumer(id).then(response => {
        this.form = response.data;
        if (this.form.delFlag === 2) {
          this.$modal.msgError(`此消费者已删除，无法操作`);
        } else{
          listInstance(this.queryInstanceParams).then(response => {
            instanceOptions = response.rows;
            this.instanceOptions = response.rows;
            this.open = true;
            this.formUpdateType = true
            this.title = "修改消费者";
          });
        }
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateConsumer(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addConsumer(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id || this.ids;

      getConsumer(id).then(response => {
        const data = response.data;
        console.log(response.data)
        if (data.delFlag === '2') {
          this.$modal.msgError(`此消费者已删除`);
        } else{
          this.$modal.confirm('是否确认删除编号为"' + id + '"的数据项？').then(function() {
            return delConsumer(id);
          }).then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          }).catch(() => {});
        }
      });
    },
  }
}
</script>

<style scoped>

</style>
